<?php
defined('IN_YuLin') || exit('NO PERMIT!');
$operation = NoBadStr($_REQUEST['operation']);
$pageShow  = '';

switch ($operation){
    case 'add_view':
        // 获取所有彩种类别信息
        $cateTab    = Table('lottery_category');
        $sql        = 'SELECT id, name FROM ' . $cateTab . ' WHERE status = 0';
        $cateList   = $db->getall($sql, 'id');
        // 获取当前库里存储的最大返点赔率设置内容
        $repointTab = Table('repoint_and_odds');
        $sql        = 'SELECT id, lott_cate_id, re_point, odds FROM ' . $repointTab . ' GROUP BY lott_cate_id ORDER BY odds DESC';
        $maxConf    = $db->getall($sql, 'lott_cate_id');
        $pageShow   = 'user/invita_view';
        break;
    case 'doadd':
        $lottArr  = $_POST['lott'];
        $mainTab  = Table('user_invita_code');
        $p1       = date('y');
        $p2       = $_SESSION['uid'];
        $p3       = substr(microtime(true), strpos(microtime(true), '.') + 1);
        $p4       = date('m');
        $p5       = date('d');
        $code     = $p4 . $p3 . $p1 . str_pad($p2, 4, 0, STR_PAD_LEFT) . $p5;
        $mainData = array(
            'uid'         => -1,     // 是后台创建的邀请码
            'invita_code' => $code,  // 邀请码
            'remark'      => NoBadStr($_POST['remark']),
            'is_once'     => NoBadStr($_POST['is_once']),
            'type'        => NoBadStr($_POST['type']),
            'createtime'  => time()
        );
        $mainInsSql  = 'INSERT INTO ' . $mainTab . ' SET ' . CreateUpdateSql($mainData);
//         echo $mainInsSql;exit;
//         $main_id     = $db->exec($mainInsSql);
        $main_id     = $db->add('user_invita_code', $mainData);
        if (!$main_id) {
            ShowMsg('操作失败，稍后重试！', -1);
            return;
        }
        $invitaItemTab = Table('user_invita_item');
        foreach ($lottArr as $k => $v){
            $ins = array(
                'code_id'      => $main_id,  // 邀请码表的id
                'lott_cate_id' => $k,        // 对应彩种类别
                're_point'     => $v,        // 设置的返点数额
            );
            $itemSql  = 'INSERT INTO ' . $invitaItemTab . ' SET ' . CreateUpdateSql($ins);
            $db->exec($itemSql);
        }
        ShowMsg('设置成功！', U('user/invita'));
        return;
        break;
    default:
        $mainTab  = Table('user_invita_code');
        $sql      = 'SELECT id, invita_code, remark, status, is_once, createtime FROM ' . $mainTab;
        $codeList = $db->getall($sql);  
        $pageShow = 'user/invita';
        break;
}

$tpl->display($pageShow);
